package com.simulation.component;

import com.google.common.eventbus.AsyncEventBus;
import com.simulation.entity.MessageEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.RejectedExecutionException;

/**
 * @author zhangxn
 * @date 2022/12/4  11:17
 */
@Slf4j
@Service
public class AsyncEventHandler {

    @Autowired
    private AsyncEventBus asyncEventBus;

    @Autowired
    private WebSocketComponent webSocketComponent;

    @PostConstruct
    public void init() {
        asyncEventBus.register(webSocketComponent);
    }

    @PreDestroy
    public void destroy() {
        asyncEventBus.unregister(webSocketComponent);
    }

    public void asyncEventPost(MessageEvent event) throws RejectedExecutionException {
        asyncEventBus.post(event);
    }
}
